﻿
//********************************************************************************//
//创建日期：<创建日期，2016-05-05>
//创建作者：<孙雪婷，sunxueting@shanghai3h.com>
//功能说明：用户信息简讯
//********************************************************************************//
using BLL;
using BLL.Access;
using DevExpress.Web;
using Entity;
using System;
using System.Data;
using System.Web.UI;
using Web.Class;

namespace Web.Pages.YongShui._12.ChaXunGL
{
    public partial class YongHuXXJX : BasePage
    {
        #region 属性

        /// <summary>
        /// 保存结果集1
        /// </summary>
        private DataTable dtYongHuXX
        {
            get
            {
                return ViewState["dtYongHuXX"] as DataTable;
            }
            set
            {
                ViewState["dtYongHuXX"] = value;
            }
        }

        /// <summary>
        /// 当前操作项ID
        /// </summary>
        private int yonghuXX_ID
        {
            get
            {
                return Convert.ToInt32(ViewState["yonghuXX_ID"]);
            }
            set
            {
                ViewState["yonghuXX_ID"] = value;
            }
        }



        #endregion

        #region 变量

        /// <summary>
        /// 用户信息
        /// </summary>
        YS_YG_YONGHUXX _YS_YG_YONGHUXX = new YS_YG_YONGHUXX();

        /// <summary>
        /// 用户信息bll
        /// </summary>
        YS_YG_YONGHUXXBLL _YS_YG_YONGHUXXBLL = new YS_YG_YONGHUXXBLL();

        /// <summary>
        /// 管理部门
        /// </summary>
        YS_JX_ZHANDIAN _YS_JX_ZHANDIAN = new YS_JX_ZHANDIAN();

        /// <summary>
        /// 管理部门bll
        /// </summary>
        YS_JX_ZHANDIANBLL _YS_JX_ZHANDIANBLL = new YS_JX_ZHANDIANBLL();

        /// <summary>
        /// 管理块
        /// </summary>
        YS_JX_GUANLIK _YS_JX_GUANLIK = new YS_JX_GUANLIK();

        /// <summary>
        /// 管理块BLL
        /// </summary>
        YS_JX_GUANLIKBLL _YS_JX_GUANLIKBLL = new YS_JX_GUANLIKBLL();

        /// <summary>
        /// 银行批量代理合同号BLL
        /// </summary>
        YS_ZW_YINHANGPLDLHTHBLL _YS_ZW_YINHANGPLDLHTHBLL = new YS_ZW_YINHANGPLDLHTHBLL();

        /// <summary>
        /// 存储过程调用类
        /// </summary>
        SpHelper _SpHelper = new SpHelper();

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitData();
            }
            this.gvYongHuXX.DataSource = this.dtYongHuXX;
            this.gvYongHuXX.DataBind();
           
        }

        /// <summary>
        /// 绑定空表头
        /// </summary>
        protected override void Render(HtmlTextWriter sender)
        {
            DataTable dt = _YS_YG_YONGHUXXBLL.SelectYS_YG_YONGHUXXesDynamic("1=0", "", null);
            base.Render(sender);
        }


        /// <summary>
        /// 查询信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnChaXun_Click(object sender, EventArgs e)
        {
            if (txtShuiLiangYM.Text != "")
            {
                LoadGridData();
                if (this.dtYongHuXX == null || this.dtYongHuXX.Rows.Count < 1)
                {
                    txtYongHuH2.Text = "";
                    txtYongHuM2.Text = "";
                    txtGuanLiBM.Text = "";
                    txtGuanLiK.Text = "";
                    txtYeWuYH.Text = "";
                    txtYeWuY.Text = "";
                    txtHeTongH.Text = "";
                    txtDiZhi.Text = "";
                    zjl.Text = "共0条";
                }
            }
        }


        /// <summary>
        /// tab1导出excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExportExcel_Click(object sender, EventArgs e)
        {
            if (this.dtYongHuXX == null || this.dtYongHuXX.Rows.Count < 1)
            {
                PageTools.AlertMsg(this, "没有符合的数据导出");
            }
            else
            {
                PageTools.ExportExcel(this.dtYongHuXX);
            }
        }

        /// <summary>
        /// 管理部门下拉改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cmbGuanLiBM_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbGuanLiK.Items.Clear();
            //cmbGuanLiK.Items.Add("全部", "");
            cmbGuanLiK.SelectedIndex = 0;
            string glbm_now = cmbGuanLiBM.Value != null ? cmbGuanLiBM.Value.ToString() : "全部";
            InitDataAgain(glbm_now);
        }

        
        #endregion

        #region 方法

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            gvYongHuXX.SettingsPager.Mode = GridViewPagerMode.EndlessPaging;
            //水量年月
            string water_y_m = BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString());
            //管理部门
            DataTable dtGuanLiBM = _YS_JX_ZHANDIANBLL.SelectYS_JX_ZHANDIANsAll();
            for (int i = 0; i < dtGuanLiBM.Rows.Count; i++)
            {
                if (dtGuanLiBM.Rows[i]["S_ZHANDIANMC"].ToString() != "中心")
                {
                    cmbGuanLiBM.Items.Add(dtGuanLiBM.Rows[i]["S_ZHANDIANMC"].ToString(), dtGuanLiBM.Rows[i]["S_ZHANDIANBH"].ToString());
                }
            }
            //业务员
            LoadYWY();
            txtShuiLiangYM.Text = water_y_m;

            int userid = LoginInfo.CurrentUser.UserID;
            string username = LoginInfo.CurrentUser.UserName;
            //根据操作人加载管理部门(站点)
            DataTable dtZhanDian = BusinessTool.GetTextAndValue("View_AS_UserInfo", "ZHANDIANBH", "ROLEID", null, string.Format("USERID={0}", userid)).Tables[0];
            string zhandian = dtZhanDian.Rows[0]["TEXT"].ToString();//站点
            string roleid = dtZhanDian.Rows[0]["VALUE"].ToString();//9001 业务员，3001 办事处主任
            if (roleid == "9001")
            {
                if (zhandian == "00")
                {
                    cmbYeWuY.Enabled = false;
                    cmbYeWuY.Text = username;
                }
                else
                {
                    cmbGuanLiBM.Enabled = false;
                    cmbYeWuY.Enabled = false;
                    cmbGuanLiBM.Value = zhandian;
                    cmbYeWuY.Text = username;
                    //管理块
                    DataTable dtGuanLiK = _YS_JX_GUANLIKBLL.SelectYS_JX_GUANLIKsDynamic(string.Format("S_ZHANDIAN={0}", zhandian), null);
                    for (int i = 0; i < dtGuanLiK.Rows.Count; i++)
                    {
                        cmbGuanLiK.Items.Add(dtGuanLiK.Rows[i]["S_GUANLIKMC"].ToString(), dtGuanLiK.Rows[i]["S_GUANLIKH"].ToString());
                    }
                }
            }
            else if (roleid == "3001" || roleid == "4001")
            {
                cmbGuanLiBM.Enabled = false;
                cmbGuanLiBM.Value = zhandian;
                //管理块
                DataTable dtGuanLiK = _YS_JX_GUANLIKBLL.SelectYS_JX_GUANLIKsDynamic(string.Format("S_ZHANDIAN={0}", zhandian), null);
                for (int i = 0; i < dtGuanLiK.Rows.Count; i++)
                {
                    cmbGuanLiK.Items.Add(dtGuanLiK.Rows[i]["S_GUANLIKMC"].ToString(), dtGuanLiK.Rows[i]["S_GUANLIKH"].ToString());
                }
                LoadYWY();
            }
        }

        /// <summary>
        /// 根据站点加载业务员
        /// </summary>
        private void LoadYWY()
        {
            DataTable dtYWY = BusinessTool.GetTextAndValue(
                "View_AS_UserInfo", "USERNAME", "USERID", null, this.cmbGuanLiBM.SelectedIndex == 0 ? "ROLENAME like '%业务员%' and department!=313" : string.Format("ZHANDIANBH='{0}' and ROLENAME like '%业务员%' and department!=313", this.cmbGuanLiBM.Value)
                 ).Tables[0];
            DataRow drTemp = dtYWY.NewRow();
            drTemp[0] = "";
            drTemp[1] = 0;
            dtYWY.Rows.InsertAt(drTemp, 0);
            PageTools.BindComboBox(
                this.cmbYeWuY, dtYWY
            );
            if (this.cmbYeWuY.Items.Count != 0)
            {
                this.cmbYeWuY.SelectedIndex = 0;
            }
        }

        /// <summary>
        /// 再次根据管理部门初始化查询条件
        /// </summary>
        /// <param name="glbm">管理部门</param>
        private void InitDataAgain(string glbm)
        {
            if (glbm != "全部")
            {
                //管理块
                DataTable dtGuanLiK = _YS_JX_GUANLIKBLL.SelectYS_JX_GUANLIKsDynamic(string.Format("S_ZHANDIAN={0}", glbm), null);
                cmbYeWuY.Items.Add("", "");
                cmbYeWuY.SelectedIndex = 0;
                for (int i = 0; i < dtGuanLiK.Rows.Count; i++)
                {
                    cmbGuanLiK.Items.Add(dtGuanLiK.Rows[i]["S_GUANLIKMC"].ToString(), dtGuanLiK.Rows[i]["S_GUANLIKH"].ToString());
                }
                //业务员
                LoadYWY();
            }
            else
            {
                cmbYeWuY.Items.Clear();
                //业务员
                LoadYWY();
            }
        }

        /// <summary>
        /// 加载管理部门
        /// </summary>
        /// <param name="ZhanDianBH">站点编号</param>
        private void LoadGuanLiBM(string ZhanDianBH)
        {
            string GuanLiBM = _YS_JX_ZHANDIANBLL.SelectYS_JX_ZHANDIANsDynamic(string.Format("S_ZHANDIANBH='{0}'", ZhanDianBH), null).Rows[0]["S_ZHANDIANMC"].ToString();
        }

        /// <summary>
        /// 加载查询数据
        /// </summary>
        private void LoadGridData()
        {
            string v_zhandian = "", v_guanlikuai = "",  v_yonghuh = "", v_yonghum = "", v_yonghudz = "";
            int v_ym = Convert.ToInt32(txtShuiLiangYM.Text);
            string v_kaohe = "0", v_yinhangqr = "";
            string v_ywy = cmbYeWuY.Value.ToString() == "0" ? "" : cmbYeWuY.Value.ToString();
            if (cmbGuanLiBM.Value != null)
            {
                v_zhandian = cmbGuanLiBM.Value.ToString();
            }
            if (cmbGuanLiK.Value != null)
            {
                v_guanlikuai = cmbGuanLiK.Value.ToString();
            }
            //包含预考核账户 20190404 add
            if (txtYuKaoH.Checked)
            {
                v_kaohe = "1";
            }

            //用户号、用户名、用户地址
            if (chkYongHuH.Checked)
            {
                v_yonghuh = txtYongHuH.Text;
            }
            else if (chkYongHuM.Checked)
            {
                v_yonghum = txtYongHuM.Text;
            }
            else if (chkYongHuDZ.Checked)
            {
                v_yonghudz = txtYongHuDZ.Text;
            }
            //银行是否确认 20190404 add
            if (chkShiFouQR.Checked && cmbShiFouQR.Text != "")
            {
                v_yinhangqr = cmbShiFouQR.Value.ToString();
            }

            this.dtYongHuXX = _SpHelper.P_YSCX_YHXXJX(v_zhandian, v_guanlikuai, v_ywy, v_ym, v_kaohe, v_yonghuh, v_yonghum, v_yonghudz, v_yinhangqr,0).Tables[0];
            gvYongHuXX.DataSource = this.dtYongHuXX;
            gvYongHuXX.DataBind();
            if (this.dtYongHuXX != null && this.dtYongHuXX.Rows.Count > 0)
            {
                zjl.Text = "共" + this.dtYongHuXX.Rows.Count.ToString() + "条";
                gvYongHuXX.FocusedRowIndex = 0;
                SetData();
            }
           
        }

        /// <summary>
        /// 给显示框赋值
        /// </summary>
        private void SetData()
        {
            DataTable dt_temp = _SpHelper.P_YSCX_YHXXJX("", "", "", Convert.ToInt32(txtShuiLiangYM.Text), "", "", "", "", "", Convert.ToInt32(gvYongHuXX.GetRowValues(0, "ID"))).Tables[0];
            if (dt_temp != null && dt_temp.Rows.Count > 0)
            {
                txtYongHuH2.Text = dt_temp.Rows[0]["用户号"].ToString();
                txtYongHuM2.Text = dt_temp.Rows[0]["用户名"].ToString();
                txtGuanLiBM.Text = dt_temp.Rows[0]["管理部门"].ToString();
                txtGuanLiK.Text = dt_temp.Rows[0]["管理块"].ToString();
                txtYeWuYH.Text = dt_temp.Rows[0]["S_YEWUY"].ToString();
                txtYeWuY.Text = dt_temp.Rows[0]["业务员"].ToString();
                txtHeTongH.Text =_SpHelper.P_YS_GETHETONGH(dt_temp.Rows[0]["用户号"].ToString()).Tables[0].Rows[0][0].ToString();
                //函数 [得到用户合同号]  得到涉水对象分类名称  得到银行支付行号
                txtDiZhi.Text = dt_temp.Rows[0]["用户地址"].ToString();
            }
        }

        /// <summary>
        /// 前台表格行改变事件回调方法
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            int id = Convert.ToInt32(this.hdCollection.Get("ID"));

            if (e.Parameter == "setinfo")
            {
                string msg = "";


                if (this.dtYongHuXX != null)
                {
                    DataTable dt_temp = _SpHelper.P_YSCX_YHXXJX("", "", "", Convert.ToInt32(txtShuiLiangYM.Text), "", "", "", "", "", id).Tables[0];

                    msg = string.Join("|", Convert.ToString(dt_temp.Rows[0]["用户号"]), Convert.ToString(dt_temp.Rows[0]["用户名"]), Convert.ToString(dt_temp.Rows[0]["管理部门"]),
                        Convert.ToString(dt_temp.Rows[0]["管理块"]), Convert.ToString(dt_temp.Rows[0]["S_YEWUY"]), Convert.ToString(dt_temp.Rows[0]["业务员"]),
                        Convert.ToString(_SpHelper.P_YS_GETHETONGH(dt_temp.Rows[0]["用户号"].ToString()).Tables[0].Rows[0][0].ToString()), Convert.ToString(dt_temp.Rows[0]["用户地址"]));

                }

                e.Result = e.Parameter + "|0|" + msg;
            }
        }
        #endregion
        
    }
}